﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using WebApplication9.TTS;

namespace WebApplication9
{
    public interface IGoogleTTSService
    {
        Task<string> TextToSpeechAsync(AudioEncoding encoding, string language, SsmlVoiceGender voiceGender, string text);
    }

    public class GoogleTTSService : HttpSendService, IGoogleTTSService
    {
        private bool Debug { get; } = false;
        public GoogleTTSService()
        {
#if DEBUG
            Debug = true;
#endif
        }

        public async Task<string> TextToSpeechAsync(AudioEncoding encoding, string language, SsmlVoiceGender voiceGender, string text)
        {            
            if (Debug)
            {
                return "The speech base64 from text!";
            }
            else
            {
                string url = $"https://content-texttospeech.googleapis.com/v1/text:synthesize?alt=json";
                int? sampleRateHertz = null;
                if (encoding != AudioEncoding.LINEAR16)
                {
                    sampleRateHertz = 24000;
                }

                var json = JsonConvert.SerializeObject(new SynthesizeRequest
                {
                    audioConfig = new AudioConfig
                    {
                        audioEncoding = encoding,
                        sampleRateHertz = sampleRateHertz,
                    },
                    voice = new VoiceSelectionParams
                    {
                        languageCode = language,
                        ssmlGender = voiceGender,
                    },
                    input = new SynthesisInput
                    {
                        text = text,
                    }
                });
                HttpRequestMessage httpRequestMessage = new HttpRequestMessage
                {
                    Content = new StringContent(json, Encoding.UTF8, "application/json"),
                };
                HttpResponseMessage httpResponseMessage = await PostAsync(httpRequestMessage, url, 60);
                string strResult = await httpResponseMessage.Content.ReadAsStringAsync();
                if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                {
                    SynthesizeResponse synthesizeResponse = JsonConvert.DeserializeObject<SynthesizeResponse>(strResult);

                    return synthesizeResponse?.audioContent;
                }
                else
                {
                    SynthesizeResponse synthesizeResponse = JsonConvert.DeserializeObject<SynthesizeResponse>(strResult);
                    throw new Exception(synthesizeResponse?.error?.message);
                }

                //string url = $"https://content-texttospeech.googleapis.com/v1/text:synthesize?alt=json&key={GoogleApiKey}";
                //using(HttpClient httpClient = new HttpClient())
                //{
                //    int? sampleRateHertz = null;
                //    if (encoding != AudioEncoding.LINEAR16)
                //    {
                //        sampleRateHertz = 24000;
                //    }

                //    var json = JsonConvert.SerializeObject(new SynthesizeRequest
                //    {
                //        audioConfig = new AudioConfig
                //        {
                //            audioEncoding = encoding,
                //            sampleRateHertz = sampleRateHertz,
                //        },
                //        voice = new VoiceSelectionParams
                //        {
                //            languageCode = language,
                //            ssmlGender = voiceGender,                            
                //        },
                //        input = new SynthesisInput
                //        {
                //            text = text,
                //        }
                //    });
                //    StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
                //    HttpResponseMessage httpResponseMessage = await httpClient.PostAsync(url, content);
                //    string strResult = await httpResponseMessage.Content.ReadAsStringAsync();
                //    if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
                //    {
                //        SynthesizeResponse synthesizeResponse = JsonConvert.DeserializeObject<SynthesizeResponse>(strResult);

                //        return synthesizeResponse?.audioContent;
                //    }
                //    else
                //    {
                //        SynthesizeResponse synthesizeResponse = JsonConvert.DeserializeObject<SynthesizeResponse>(strResult);
                //        throw new Exception(synthesizeResponse?.error?.message);
                //    }
                //}

            }

        }
    }
}
